//
//  NetworkTools+UserAccount.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/25.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import Foundation

extension NetworkTools{

   //加载获取assess_token
    func loadAssess_token(code:String,finished:([String:AnyObject]?)->()){
    
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let paramas = ["client_id":appKey,
            "client_secret":appSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":redirectUri]
        
        // [weak self] 不加也没事，因为是单列  只有程序挂掉之后，单列才会销毁，只是为了规范
        request(.POST, URLString: urlString, parameters: paramas){[weak self]
            result  in
            
            guard let result = result,
                let access_token = result["access_token"] as?String,
                let uid = result["uid"] as?String
                else{
                    
                    print("数据格式不正确")
                    finished(nil)  //一坑  这不写，就无法展示错误
                    return
            }

        
          self!.loadUser(access_token, uid: uid, finished: { (UserDict) -> () in
            
            guard var UserDict = UserDict else{
                
                print("数据错误")
                return
            }
            //合并字典   不合并的话 打印account  是不会得到定义字典想要的key  value值的
            for (k,v) in result {
                
                UserDict[k] = v
            }
            
            //回调
            finished(UserDict)
   
          })

        }
    }
    
    //加载用户的信息
    
    func loadUser(access_token:String,uid:String,finished:([String:AnyObject]?)->()){
    
        let urlString = "https://api.weibo.com/2/users/show.json"
        let paramas = ["access_token":access_token,"uid":uid]
        
        request(.GET, URLString: urlString, parameters: paramas, finished:finished)
    
    }
    
    


}